package com.linpq.road.service;

import java.util.List;
import com.linpq.common.core.service.CrudService;
import com.linpq.common.exception.BusinessException;
import com.linpq.common.utils.DateUtils;
import com.linpq.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linpq.road.mapper.RdHighSpeedRoadMapper;
import com.linpq.road.domain.RdHighSpeedRoad;
import com.linpq.common.core.text.Convert;
import com.linpq.common.core.service.BaseService;
/**
 * 高速公路Service业务层处理
 *
 * @author linpq
 * @date 2021-07-28
 */
@Service
public class RdHighSpeedRoadService  extends CrudService<RdHighSpeedRoadMapper, RdHighSpeedRoad> {

    /**
     * 查询高速公路
     *
     * @param id 高速公路ID
     * @return 高速公路
     */

    public RdHighSpeedRoad selectRdHighSpeedRoadById(String id) {
        return dao.get(id);
    }

    /**
     * 查询高速公路列表
     *
     * @param rdHighSpeedRoad 高速公路
     * @return 高速公路
     */

    public List<RdHighSpeedRoad> selectRdHighSpeedRoadList(RdHighSpeedRoad rdHighSpeedRoad) {
        return dao.findList(rdHighSpeedRoad);
    }

    /**
     * 新增高速公路
     *
     * @param rdHighSpeedRoad 高速公路
     * @return 结果
     */

    public int insertRdHighSpeedRoad(RdHighSpeedRoad rdHighSpeedRoad) {
        rdHighSpeedRoad.setCreateTime(DateUtils.getNowDate());
        return super.save(rdHighSpeedRoad);
    }

    /**
     * 修改高速公路
     *
     * @param rdHighSpeedRoad 高速公路
     * @return 结果
     */

    public int updateRdHighSpeedRoad(RdHighSpeedRoad rdHighSpeedRoad) {
                rdHighSpeedRoad.setUpdateTime(DateUtils.getNowDate());
        return super.save(rdHighSpeedRoad);
    }

    /**
     * 删除高速公路对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */

    public int deleteRdHighSpeedRoadByIds(String ids) {
        return dao.deleteRdHighSpeedRoadByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除高速公路信息
     *
     * @param id 高速公路ID
     * @return 结果
     */

    public int deleteRdHighSpeedRoadById(String id) {
        return dao.delete(id);
    }

    /**
     * 导入高速公路数据
     *
     * @param rdHighSpeedRoadList 高速公路数据列表
     * @param isUpdateSupport     是否存在，再则覆盖
     * @param operName            操作用户
     * @return
     */
    public String importHighSpeedRoad(List<RdHighSpeedRoad> rdHighSpeedRoadList, Boolean isUpdateSupport, String operName) {
        if (!StringUtils.isNotEmpty(rdHighSpeedRoadList) && rdHighSpeedRoadList.size() == 0) {
            throw new BusinessException("导入用户不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RdHighSpeedRoad r : rdHighSpeedRoadList) {
            try {
                RdHighSpeedRoad rdHighSpeedRoad = dao.selectHighSpeedByLoginName(r.getName());
                if (StringUtils.isNull(rdHighSpeedRoad)) {
                    r.setName(r.getName());
                    this.insertRdHighSpeedRoad(r);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "高速公路信息" + r.getName() + "导入成功");
                } else if (isUpdateSupport) {
                    this.updateRdHighSpeedRoad(r);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "高速公路信息 " + r.getName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "高速公路信息" + r.getName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "高速公路信息" + r.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
